<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/xhtml;charset=utf-8" http-equiv="Content-Type"/>
<meta content="IE=9" http-equiv="X-UA-Compatible"/>
<meta content="Doxygen 1.8.13" name="generator"/>
<title>OpenCV: Clustering</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script src="../../jquery.js" type="text/javascript"></script>
<script src="../../dynsections.js" type="text/javascript"></script>
<script src="../../tutorial-utils.js" type="text/javascript"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script src="../../search/searchdata.js" type="text/javascript"></script>
<script src="../../search/search.js" type="text/javascript"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js" type="text/javascript"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css"/>
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellpadding="0" cellspacing="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
    <span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script src="../../menudata.js" type="text/javascript"></script>
<script src="../../menu.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow" onkeydown="return searchBox.OnSearchSelectKey(event)" onmouseout="return searchBox.OnSearchSelectHide()" onmouseover="return searchBox.OnSearchSelectShow()">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe frameborder="0" id="MSearchResults" name="MSearchResults" src="javascript:void(0)">
</iframe>
</div>
<div class="header">
  <div class="summary">
<a href="#enum-members">Enumerations</a> |
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">Clustering<div class="ingroups"><a class="el" href="../../d0/de1/group__core.html">Core functionality</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="enum-members"></a>
Enumerations</h2></td></tr>
<tr class="memitem:ga276000efe55ee2756e0c471c7b270949"><td align="right" class="memItemLeft" valign="top">enum  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d38/group__core__cluster.html#ga276000efe55ee2756e0c471c7b270949">cv::KmeansFlags</a> { <br/>
  <a class="el" href="../../d5/d38/group__core__cluster.html#gga276000efe55ee2756e0c471c7b270949adfa80a38dfc0aef0de888c3164f33faf">cv::KMEANS_RANDOM_CENTERS</a> = 0, 
<br/>
  <a class="el" href="../../d5/d38/group__core__cluster.html#gga276000efe55ee2756e0c471c7b270949a78ddd00a99cd51db10ed63c024eb1e62">cv::KMEANS_PP_CENTERS</a> = 2, 
<br/>
  <a class="el" href="../../d5/d38/group__core__cluster.html#gga276000efe55ee2756e0c471c7b270949a40625baa3d28c780813f9634b960b366">cv::KMEANS_USE_INITIAL_LABELS</a> = 1
<br/>
 }<tr class="memdesc:ga276000efe55ee2756e0c471c7b270949"><td class="mdescLeft"> </td><td class="mdescRight">k-Means flags  <a href="../../d5/d38/group__core__cluster.html#ga276000efe55ee2756e0c471c7b270949">More...</a><br/></td></tr>
</td></tr>
<tr class="separator:ga276000efe55ee2756e0c471c7b270949"><td class="memSeparator" colspan="2"> </td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ga9a34dc06c6ec9460e90860f15bcd2f88"><td align="right" class="memItemLeft" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/d38/group__core__cluster.html#ga9a34dc06c6ec9460e90860f15bcd2f88">cv::kmeans</a> (<a class="el" href="../../dc/d84/group__core__basic.html#ga353a9de602fe76c709e12074a6f362ba">InputArray</a> data, int K, <a class="el" href="../../dc/d84/group__core__basic.html#gaf77c9a14ef956c50c1efd4547f444e63">InputOutputArray</a> bestLabels, <a class="el" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a> criteria, int attempts, int flags, <a class="el" href="../../dc/d84/group__core__basic.html#gaad17fda1d0f0d1ee069aebb1df2913c0">OutputArray</a> centers=<a class="el" href="../../dc/d84/group__core__basic.html#gad9287b23bba2fed753b36ef561ae7346">noArray</a>())</td></tr>
<tr class="memdesc:ga9a34dc06c6ec9460e90860f15bcd2f88"><td class="mdescLeft"> </td><td class="mdescRight">Finds centers of clusters and groups input samples around the clusters.  <a href="../../d5/d38/group__core__cluster.html#ga9a34dc06c6ec9460e90860f15bcd2f88">More...</a><br/></td></tr>
<tr class="separator:ga9a34dc06c6ec9460e90860f15bcd2f88"><td class="memSeparator" colspan="2"> </td></tr>
<tr class="memitem:ga2037c989e69b499c1aa271419f3a9b34"><td class="memTemplParams" colspan="2">template&lt;typename _Tp , class _EqPredicate &gt; </td></tr>
<tr class="memitem:ga2037c989e69b499c1aa271419f3a9b34"><td align="right" class="memTemplItemLeft" valign="top">int </td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d5/d38/group__core__cluster.html#ga2037c989e69b499c1aa271419f3a9b34">partition</a> (const std::vector&lt; _Tp &gt; &amp;_vec, std::vector&lt; int &gt; &amp;labels, _EqPredicate predicate=_EqPredicate())</td></tr>
<tr class="memdesc:ga2037c989e69b499c1aa271419f3a9b34"><td class="mdescLeft"> </td><td class="mdescRight">Splits an element set into equivalency classes.  <a href="#ga2037c989e69b499c1aa271419f3a9b34">More...</a><br/></td></tr>
<tr class="separator:ga2037c989e69b499c1aa271419f3a9b34"><td class="memSeparator" colspan="2"> </td></tr>
</table>
<a id="details" name="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Enumeration Type Documentation</h2>
<a id="ga276000efe55ee2756e0c471c7b270949"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga276000efe55ee2756e0c471c7b270949">◆ </a></span>KmeansFlags</h2>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">enum <a class="el" href="../../d5/d38/group__core__cluster.html#ga276000efe55ee2756e0c471c7b270949">cv::KmeansFlags</a></td>
        </tr>
      </table>
</div><div class="memdoc">
<p><code>#include &lt;<a class="el" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</code></p>
<p>k-Means flags </p>
<table class="fieldtable">
<tr><th colspan="2">Enumerator</th></tr><tr><td class="fieldname"><a id="gga276000efe55ee2756e0c471c7b270949adfa80a38dfc0aef0de888c3164f33faf"></a>KMEANS_RANDOM_CENTERS <div class="python_language">Python: cv.KMEANS_RANDOM_CENTERS</div></td><td class="fielddoc"><p>Select random initial centers in each attempt. </p>
</td></tr>
<tr><td class="fieldname"><a id="gga276000efe55ee2756e0c471c7b270949a78ddd00a99cd51db10ed63c024eb1e62"></a>KMEANS_PP_CENTERS <div class="python_language">Python: cv.KMEANS_PP_CENTERS</div></td><td class="fielddoc"><p>Use kmeans++ center initialization by Arthur and Vassilvitskii [Arthur2007]. </p>
</td></tr>
<tr><td class="fieldname"><a id="gga276000efe55ee2756e0c471c7b270949a40625baa3d28c780813f9634b960b366"></a>KMEANS_USE_INITIAL_LABELS <div class="python_language">Python: cv.KMEANS_USE_INITIAL_LABELS</div></td><td class="fielddoc"><p>During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of KMEANS_*_CENTERS flag to specify the exact method. </p>
</td></tr>
</table>
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a id="ga9a34dc06c6ec9460e90860f15bcd2f88"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga9a34dc06c6ec9460e90860f15bcd2f88">◆ </a></span>kmeans()</h2>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">double cv::kmeans </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="../../dc/d84/group__core__basic.html#ga353a9de602fe76c709e12074a6f362ba">InputArray</a> </td>
          <td class="paramname"><em>data</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int </td>
          <td class="paramname"><em>K</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="../../dc/d84/group__core__basic.html#gaf77c9a14ef956c50c1efd4547f444e63">InputOutputArray</a> </td>
          <td class="paramname"><em>bestLabels</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a> </td>
          <td class="paramname"><em>criteria</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int </td>
          <td class="paramname"><em>attempts</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">int </td>
          <td class="paramname"><em>flags</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype"><a class="el" href="../../dc/d84/group__core__basic.html#gaad17fda1d0f0d1ee069aebb1df2913c0">OutputArray</a> </td>
          <td class="paramname"><em>centers</em> = <code><a class="el" href="../../dc/d84/group__core__basic.html#gad9287b23bba2fed753b36ef561ae7346">noArray</a>()</code> </td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table><table class="python_language"><tr><th colspan="999" style="text-align:left">Python:</th></tr><tr><td style="width: 20px;"></td><td>retval, bestLabels, centers</td><td>=</td><td>cv.kmeans(</td><td class="paramname">data, K, bestLabels, criteria, attempts, flags[, centers]</td><td>)</td></tr></table>
</div><div class="memdoc">
<p><code>#include &lt;<a class="el" href="../../d0/d9c/core_2include_2opencv2_2core_8hpp.html">opencv2/core.hpp</a>&gt;</code></p>
<p>Finds centers of clusters and groups input samples around the clusters. </p>
<p>The function kmeans implements a k-means algorithm that finds the centers of cluster_count clusters and groups the input samples around the clusters. As an output, \(\texttt{bestLabels}_i\) contains a 0-based cluster index for the sample stored in the \(i^{th}\) row of the samples matrix.</p>
<dl class="section note"><dt>Note</dt><dd><ul>
<li>(Python) An example on K-means clustering can be found at opencv_source_code/samples/python/kmeans.py </li>
</ul>
</dd></dl>
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">data</td><td>Data for clustering. An array of N-Dimensional points with float coordinates is needed. Examples of this array can be:<ul>
<li><a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">Mat</a> points(count, 2, CV_32F);</li>
<li><a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">Mat</a> points(count, 1, CV_32FC2);</li>
<li><a class="el" href="../../d3/d63/classcv_1_1Mat.html" title="n-dimensional dense array class ">Mat</a> points(1, count, CV_32FC2);</li>
<li>std::vector&lt;<a class="el" href="../../dc/d84/group__core__basic.html#ga7d080aa40de011e4410bca63385ffe2a">cv::Point2f</a>&gt; points(sampleCount); </li>
</ul>
</td></tr>
    <tr><td class="paramname">K</td><td>Number of clusters to split the set by. </td></tr>
    <tr><td class="paramname">bestLabels</td><td>Input/output integer array that stores the cluster indices for every sample. </td></tr>
    <tr><td class="paramname">criteria</td><td>The algorithm termination criteria, that is, the maximum number of iterations and/or the desired accuracy. The accuracy is specified as criteria.epsilon. As soon as each of the cluster centers moves by less than criteria.epsilon on some iteration, the algorithm stops. </td></tr>
    <tr><td class="paramname">attempts</td><td>Flag to specify the number of times the algorithm is executed using different initial labellings. The algorithm returns the labels that yield the best compactness (see the last function parameter). </td></tr>
    <tr><td class="paramname">flags</td><td>Flag that can take values of <a class="el" href="../../d5/d38/group__core__cluster.html#ga276000efe55ee2756e0c471c7b270949" title="k-Means flags ">cv::KmeansFlags</a> </td></tr>
    <tr><td class="paramname">centers</td><td>Output matrix of the cluster centers, one row per each cluster center. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>The function returns the compactness measure that is computed as <p class="formulaDsp">
\[\sum _i \| \texttt{samples} _i - \texttt{centers} _{ \texttt{labels} _i} \| ^2\]
</p>
 after every attempt. The best (minimum) value is chosen and the corresponding labels and the compactness value are returned by the function. Basically, you can use only the core of the function, set the number of attempts to 1, initialize labels each time using a custom algorithm, pass them with the ( flags = <a class="el" href="../../d5/d38/group__core__cluster.html#gga276000efe55ee2756e0c471c7b270949a40625baa3d28c780813f9634b960b366">KMEANS_USE_INITIAL_LABELS</a> ) flag, and then choose the best (most-compact) clustering. </dd></dl>
<dl><dt><b>Examples: </b></dt><dd><a class="el" href="../../d9/dde/samples_2cpp_2kmeans_8cpp-example.html#a17">samples/cpp/kmeans.cpp</a>.</dd>
</dl>
</div>
</div>
<a id="ga2037c989e69b499c1aa271419f3a9b34"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga2037c989e69b499c1aa271419f3a9b34">◆ </a></span>partition()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;typename _Tp , class _EqPredicate &gt; </div>
      <table class="memname">
        <tr>
          <td class="memname">int partition </td>
          <td>(</td>
          <td class="paramtype">const std::vector&lt; _Tp &gt; &amp; </td>
          <td class="paramname"><em>_vec</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">std::vector&lt; int &gt; &amp; </td>
          <td class="paramname"><em>labels</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">_EqPredicate </td>
          <td class="paramname"><em>predicate</em> = <code>_EqPredicate()</code> </td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<p><code>#include &lt;<a class="el" href="../../d6/d00/operations_8hpp.html">opencv2/core/operations.hpp</a>&gt;</code></p>
<p>Splits an element set into equivalency classes. </p>
<p>The generic function partition implements an \(O(N^2)\) algorithm for splitting a set of \(N\) elements into one or more equivalency classes, as described in <a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">http://en.wikipedia.org/wiki/Disjoint-set_data_structure</a> . The function returns the number of equivalency classes. </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">_vec</td><td>Set of elements stored as a vector. </td></tr>
    <tr><td class="paramname">labels</td><td>Output vector of labels. It contains as many elements as vec. Each label labels[i] is a 0-based cluster index of <code>vec[i]</code>. </td></tr>
    <tr><td class="paramname">predicate</td><td>Equivalence predicate (pointer to a boolean function of two arguments or an instance of the class that has the method bool operator()(const _Tp&amp; a, const _Tp&amp; b) ). The predicate returns true when the elements are certainly in the same class, and returns false if they may or may not be in the same class. </td></tr>
  </table>
  </dd>
</dl>
</div>
</div>
</div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:40 for OpenCV by  <a href="http://www.doxygen.org/index.html">
<img alt="doxygen" class="footer" src="../../doxygen.png"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
